<?php
namespace app\index\model;
use think\Db;
use think\Model;
class Comment extends Model
{
    // 表名
    protected $name = 'news_comment';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = "create_time";
    protected $updateTime = "update_time";
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time',
        'memberinfo',
        'parentinfo',
    ];
    
    public function getCreateTimeAttr($value, $data)
    {
        $time_value=time()-$value;
        if($time_value<60){
            return "刚刚";
        }else if($time_value>60&&$time_value<3600){
            return ceil($time_value/60)."分钟前";
        }else if($time_value>3600&&$value>strtotime(date("Y-m-d"))){
            return ceil($time_value/3600)."小时前";
        }else{
            return date("Y-m-d",$value);
        }
    }
    public function getMemberinfoAttr($value,$data){
       return db("members")->field("member_id,nickname,avatar,sign_text")->where("member_id",$data['member_id'])->find();
    }
    public function getParentinfoAttr($value,$data){
      return db('members')->field('member_id,nickname,avatar')->where('member_id',$data['parent_id'])->find();
    }

}
